//
//  WebViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/7/2.
//

#import "WebViewController.h"
#import <WebKit/WebKit.h>
#import <Masonry/Masonry.h>

@interface WebViewController () <WKNavigationDelegate>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *loadingIndicator;

@end

@implementation WebViewController

- (void)viewDidLoad {
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.extendedLayoutIncludesOpaqueBars = YES;
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    // 顶部自定义导航栏视图
    UIView *navBar = [[UIView alloc] init];
    navBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navBar];
    [navBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(88); // 状态栏+导航栏
    }];

    // 返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.backgroundColor = [UIColor whiteColor];
    [backBtn setTitle:@"2132131" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"next"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(16);
        make.top.equalTo(self.view.mas_top).offset(40);
        make.width.height.mas_equalTo(32);
        
    }];

    // 标题（可选）
    if (self.titleString) {
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = self.titleString;
        titleLabel.font = [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = [UIColor blackColor];
        [navBar addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(navBar);
            make.centerY.equalTo(backBtn);
        }];
    }

    // WKWebView
    WKWebView *webView = [[WKWebView alloc] init];
    [self.view addSubview:webView];
    [webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(navBar.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    if (self.urlString) {
        NSURL *url = [NSURL URLWithString:self.urlString];
        if (url) {
            [webView loadRequest:[NSURLRequest requestWithURL:url]];
        }
    }

    // 加载指示器
    self.loadingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.loadingIndicator.hidesWhenStopped = YES;
    [self.view addSubview:self.loadingIndicator];
    
    [self setupConstraints];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)setupConstraints {
    // 获取导航区高度
    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    CGFloat navBarHeight = 44.0;
    CGFloat topBarHeight = statusBarHeight + navBarHeight;
    
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(topBarHeight);
    }];
    
//   
//    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.headerView);
//        make.top.equalTo(self.headerView).offset(topBarHeight/2+20);
//    }];
   
    
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    [self.loadingIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.webView);
    }];
}

- (void)loadWebContent {
    if (!self.urlString || [self.urlString length] == 0) {
        NSLog(@"URL为空，无法加载网页");
        return;
    }
    
    NSURL *url = [NSURL URLWithString:self.urlString];
    if (!url) {
        NSLog(@"无效的URL: %@", self.urlString);
        return;
    }
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    [self.loadingIndicator startAnimating];
}

#pragma mark - Actions

- (void)backBtnTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - WKNavigationDelegate

- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [self.loadingIndicator startAnimating];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self.loadingIndicator stopAnimating];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [self.loadingIndicator stopAnimating];
    NSLog(@"网页加载失败: %@", error.localizedDescription);
}

@end 
